package models

import (
	_ "fmt"

	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

type OptionsQueryParam struct {
	BaseQueryParam
	Filter string
	Typeid string
	Status string
	Depid  int64
}

type Options struct {
	Id            int64
	DepartmentId  int64
	OptionName    string
	OptionKey     string
	OptionContent string
	Typeid        int
	Pinyin        string
	Status        int
}

func OptionsTableName() string {
	return TableName("set_options")
}

func (a *Options) TableName() string {
	return OptionsTableName()
}

//返回配置主信息
func OptionsParentType() map[int]string {

	options := make(map[int]string)

	options[10] = "医生"
	options[11] = "验光师"
	options[12] = "验光类型"
	options[20] = "营业员"
	options[40] = "加工师"
	return options
}

func OptionsList(params *OptionsQueryParam) []*Options {

	query := orm.NewOrm().QueryTable(OptionsTableName())
	lists := make([]*Options, 0)

	if len(params.Filter) > 0 {
		query = query.Filter("option_name__istartswith", params.Filter)
	}
	if len(params.Typeid) > 0 {
		query = query.Filter("typeid", params.Typeid)
	}

	if len(params.Status) > 0 {
		query = query.Filter("status", params.Status)
	}

	query = query.Filter("department_id", params.Depid)

	query.All(&lists)

	return lists
}

func OptionsGetById(id int) (*Options, error) {

	r := new(Options)
	err := orm.NewOrm().QueryTable(OptionsTableName()).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}

	return r, nil
}

func OptionsAdd(a *Options) (int64, error) {

	a.Pinyin = utils.ConvertPinyin(a.OptionName)

	return orm.NewOrm().Insert(a)
}

func (a *Options) OptionsUpdate(fields ...string) error {
	a.Pinyin = utils.ConvertPinyin(a.OptionName)
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}
